package cn.lili.modules.serviceimpl;

import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.goods.client.GoodsClient;
import cn.lili.modules.goods.entity.enums.GoodsMarketEnum;
import cn.lili.modules.operationcenter.entity.dos.OperationCenter;
import cn.lili.modules.operationcenter.entity.enums.DateTypeEnum;
import cn.lili.modules.operationcenter.entity.vos.OperationCenterSupplierVo;
import cn.lili.modules.operationcenter.entity.vos.UrbanStationSearchParams;
import cn.lili.modules.service.OperationCenterService;
import cn.lili.modules.service.UrbanStationSupplierService;
import cn.lili.modules.store.client.StoreClient;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.modules.system.client.IRegionClient;
import cn.lili.modules.system.entity.dos.Region;
import cn.lili.modules.urbansubstation.client.UrbanSubstationClient;
import cn.lili.modules.urbansubstation.entity.dos.UrbanSubstation;
import cn.lili.modules.urbansubstation.entity.dto.UrbanSubstationSearchPageParams;
import cn.lili.modules.urbansubstation.entity.dto.UrbanSubstationSearchParams;
import cn.lili.modules.utils.DateUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class UrbanStationSupplierServiceImpl implements UrbanStationSupplierService {

    private final OperationCenterService operationCenterService;

    private final UrbanSubstationClient urbanSubstationClient;

    private final IRegionClient regionClient;

    private final StoreClient storeClient;

    private final GoodsClient goodsClient;

    /**
     * 分页获取城市分站服务商信息
     * @param searchParams
     * @param page
     * @return
     */
    @Override
    public Page<UrbanSubstation> getUrbanByPage (UrbanSubstationSearchParams searchParams, PageVO page) {
        if (null != UserContext.getCurrentUser()) {
            // 先获取省份
            OperationCenter operationCenter = operationCenterService.getOperationCenter(Objects.requireNonNull(UserContext.getCurrentUser()).getId());
            if (null != operationCenter) {
                // 获取城市分站
                UrbanSubstationSearchPageParams searchPageParams = new UrbanSubstationSearchPageParams();
                searchPageParams.setPage(page);
                searchParams.setProvinceId(operationCenter.getProvinceId());
                searchPageParams.setUrbanSubstationSearchParams(searchParams);
                Page<UrbanSubstation> resultPage = urbanSubstationClient.getByRegionIds(searchPageParams);
                if (!resultPage.getRecords().isEmpty()) {
                    resultPage.getRecords().forEach(urbanSubstation -> {
                        // 供货商数量
                        List<Store> storeList = storeClient.getSupplierListByUrbanId(urbanSubstation.getUserId());
                        urbanSubstation.setSupplierCount(storeList.size());
                        if (!storeList.isEmpty()) {
                            List<String> storeIds = storeList.stream().map(Store::getId).toList();
                            // 入驻商品数量
                            long goodsCount = goodsClient.getGoodsCountByStoreIds(storeIds, null);
                            urbanSubstation.setSupplierGoodsCount(goodsCount);
                            // 本月供应商商品数量
                            long goodsCountByMonth = goodsClient.getCurrentMonthGoodsCountByStoreIds(storeIds);
                            urbanSubstation.setAddGoodsCount(goodsCountByMonth);

                            // 上架商品数量
                            long upperGoodsCount = goodsClient.getGoodsCountByStoreIds(storeIds, GoodsMarketEnum.UPPER.name());
                            urbanSubstation.setUpperGoodsCount(upperGoodsCount);

                            // 违规下架商品数量
                            long downGoodsCount = goodsClient.getGoodsCountByStoreIds(storeIds, GoodsMarketEnum.DOWN.name());
                            urbanSubstation.setDownGoodsCount(downGoodsCount);
                        }else {
                            urbanSubstation.setSupplierGoodsCount(0L);
                            urbanSubstation.setSupplierCountByMonth(0L);
                            urbanSubstation.setUpperGoodsCount(0L);
                            urbanSubstation.setDownGoodsCount(0L);
                            urbanSubstation.setAddGoodsCount(0L);
                        }

                        // 本月新增供货商数量
                        Long monthSupplierCount = storeClient.getCurrentMonthSupplierCountByUrbanId(urbanSubstation.getUserId());
                        urbanSubstation.setSupplierCountByMonth(monthSupplierCount);
                    });
                }
                return resultPage;
            }
        }
        return null;
    }

    @Override
    public Long getUrbanSupplierCount (UrbanStationSearchParams params) {
        UrbanSubstation urbanSubstation = urbanSubstationClient.getById(params.getUrbanId());
        if (null != urbanSubstation) {
            if (StringUtils.isNotEmpty(params.getDateTypeEnum())) {
                Date[] dateArray = DateUtils.getDateArray(DateTypeEnum.valueOf(params.getDateTypeEnum()));
                params.setStartTime(dateArray[0]);
                params.setEndTime(dateArray[1]);
                params.setUrbanId(urbanSubstation.getUserId());
            }
            return storeClient.getSupplierListByParams(params);
        }
        return 0L;
    }

    @Override
    public Long getUrbanGoodsCount (UrbanStationSearchParams params) {
        UrbanSubstation urbanSubstation = urbanSubstationClient.getById(params.getUrbanId());
        if (null != urbanSubstation) {
            if (StringUtils.isNotEmpty(params.getDateTypeEnum())) {
                Date[] dateArray = DateUtils.getDateArray(DateTypeEnum.valueOf(params.getDateTypeEnum()));
                params.setStartTime(dateArray[0]);
                params.setEndTime(dateArray[1]);
            }

            List<Store> storeList = storeClient.getSupplierListByUrbanId(urbanSubstation.getUserId());
            if (!storeList.isEmpty()) {
                List<String> storeIds = storeList.stream().map(Store::getId).toList();
                params.setStoreIds(storeIds);
                return goodsClient.getGoodsCountByParams(params);
            }
        }
        return 0L;
    }

    /**
     * 分页获取供货商信息
     * @param page
     * @return
     */
    public Page<OperationCenterSupplierVo> getSupplierByPage (String supplierName, String urbanId, PageVO page) {
        //查营中心下的供应商：
        //lilishop_urban_substation.li_urban_substation  城市站点表
        //运营中心账号下的城市分站 priviceId --->城市 分站id = li_stoer.urban_user_id
        //下架记录  li_goods_down_record  -->  supplier_id 供应商id
        if (null != UserContext.getCurrentUser()) {
            OperationCenter operationCenter = operationCenterService.getOperationCenter(UserContext.getCurrentId());
            if (null != operationCenter) {
                // 根据省份获取城市
//                List<Region> regionList = regionClient.getRegionByParentId(operationCenter.getProvinceId());
//                if (!regionList.isEmpty()) {
//
//                }
//            operationCenter.getProvinceId()

            }
        }
        return null;
    }
}
